package com.cn.medicalcare.controller.backstageservlet.imgservlet;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.service.UpLoadImgService;
import com.cn.medicalcare.service.serviceimpl.UpLoadImgServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/multipleuploadimg.json")
@MultipartConfig(maxFileSize = 5 * 1024 * 1024)
public class    MultipleUoloadServlet extends HttpServlet {

    private UpLoadImgService upLoadImgService = UpLoadImgServiceImpl.getInstance();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Collection<Part> list = req.getParts();
        String imgs = "";
        Map<String, Object> map = new LinkedHashMap<>();
        for (Part p : list) {
            String pathandname = upLoadImgService.upLoadImg(p);
            if (pathandname != null)
                imgs += UpLoadImgService.IMGSPLIT + pathandname;
        }
        if (imgs.length() > 0) {
            map.put("status", 200);
            map.put("srcs", imgs);
        } else {
            map.put("status", 500);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }
}
